#pragma once
#include "tool.hpp"
#include <mutex>

namespace cloud
{
#define CONFIG_FILE "../src/server/config.conf"
    class config
    {
    private:
        // 配置信息
        size_t _hot_time;           // 热点文件过期时间
        size_t _server_port;        // 服务器端口
        std::string _server_ip;     // 服务器ip
        std::string _suffix_pack;   // 压缩后缀
        std::string _url_download;  // 下载文件路径
        std::string _url_packdir;   // 压缩文件路径
        std::string _url_backup;    // 备份文件路径
        std::string _data_cloud;    // 数据信息文件

        // 懒汉模式
        static std::mutex _mutex;
        static config *_instance;
        config()
        {
            getConfigFile();
        }

    private:
        void getConfigFile()
        {
            FileTool ft(CONFIG_FILE);
            std::string content;

            if (ft.getAllContent(&content) == false)
            {
                std::cerr << "config.hpp getConfigFile getAllContent error" << std::endl;
            }
            Json::Value root;
            jsonTool::deserialize(content, &root);

            _hot_time = root["hot_time"].asUInt();
            _server_port = root["server_port"].asUInt();
            _server_ip = root["server_ip"].asString();
            _suffix_pack = root["suffix_pack"].asString();
            _url_download = root["url_download"].asString();
            _url_packdir = root["url_packdir"].asString();
            _url_backup = root["url_backup"].asString();
            _data_cloud = root["data_cloud"].asString();
        }

    public:
        static config *getInstance()
        {
            if (_instance == nullptr)
            {
                _mutex.lock();
                if (_instance == nullptr)
                {
                    _instance = new config;
                }
                _mutex.unlock();
            }
            return _instance;
        }

        size_t getHotTime()
        {
            return _hot_time;
        }

        size_t getServerPort()
        {
            return _server_port;
        }

        std::string getServerIP()
        {
            return _server_ip;
        }

        std::string getSuffixPack()
        {
            return _suffix_pack;
        }

        std::string getUrlDownload()
        {
            return _url_download;
        }

        std::string getUrlPackdir()
        {
            return _url_packdir;
        }

        std::string getUrlBackup()
        {
            return _url_backup;
        }

        std::string getDataCloud()
        {
            return _data_cloud;
        }
    };

    config *config::_instance = nullptr;
    std::mutex config::_mutex;
}